using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CookComputing.XmlRpc;
using System.Collections.Generic;
using SocialShare.Web.Integrations;
using SocialShare.BusinessEntities;
using SocialShare.DAL.Facades;
using Utilities;

namespace SocialShare.Web.Integrations
{
    public class MetaWeblog : XmlRpcService, IMetaWeblog
    {
        #region Public Constructors

        public MetaWeblog() { }

        #endregion

        #region IMetaWeblog Members

        string IMetaWeblog.AddPost(string blogid, string username, string password, Post post, bool publish)
        {

            if (ValidateUser(username, password))
            {

                string id = string.Empty;



                // TODO: Implement your own logic to add the post and set the id



                return id;

            }

            throw new XmlRpcFaultException(0, "User is not valid!");

        }

        bool IMetaWeblog.UpdatePost(string postid, string username, string password, Post post, bool publish)
        {

            if (ValidateUser(username, password))
            {

                bool result = false;



                // TODO: Implement your own logic to add the post and set the result



                return result;

            }

            throw new XmlRpcFaultException(0, "User is not valid!");

        }

        Post IMetaWeblog.GetPost(string postid, string username, string password)
        {

            if (ValidateUser(username, password))
            {

                Post post = new Post();



                // TODO: Implement your own logic to update the post and set the post



                return post;

            }

            throw new XmlRpcFaultException(0, "User is not valid!");

        }

        CategoryInfo[] IMetaWeblog.GetCategories(string blogid, string username, string password)
        {

            if (ValidateUser(username, password))
            {

                List<CategoryInfo> categoryInfos = new List<CategoryInfo>();

                
                // TODO: Implement your own logic to get category info and set the categoryInfos
                
                return categoryInfos.ToArray();

            }

            throw new XmlRpcFaultException(0, "User is not valid!");

        }

        Post[] IMetaWeblog.GetRecentPosts(string blogid, string username, string password, int numberOfPosts)
        {

            if (ValidateUser(username, password))
            {

                List<Post> posts = new List<Post>();



                // TODO: Implement your own logic to get posts and set the posts



                return posts.ToArray();

            }

            throw new XmlRpcFaultException(0, "User is not valid!");

        }

        MediaObjectInfo IMetaWeblog.NewMediaObject(string blogid, string username, string password, MediaObject mediaObject)
        {

            if (ValidateUser(username, password))
            {

                MediaObjectInfo objectInfo = new MediaObjectInfo();



                // TODO: Implement your own logic to add media object and set the objectInfo



                return objectInfo;

            }

            throw new XmlRpcFaultException(0, "User is not valid!");

        }

        bool IMetaWeblog.DeletePost(string key, string postid, string username, string password, bool publish)
        {

            if (ValidateUser(username, password))
            {

                bool result = true;
                StrategyProxyUser proxyUser = GetUser(username, password);
                BlogFacade blogFacade = new BlogFacade();
                blogFacade.DeleteBlog(postid);
                return result;
            }

            throw new XmlRpcFaultException(0, "User is not valid!");

        }

        BlogInfo[] IMetaWeblog.GetUsersBlogs(string key, string username, string password)
        {

            if (ValidateUser(username, password))
            {

                List<BlogInfo> infoList = new List<BlogInfo>();

                StrategyProxyUser proxyUser = GetUser(username, password);
                MemberFacade memFacde = new MemberFacade();

                SocialShare_MemberProfiles blog = memFacde.GetUserProfileByID(proxyUser.User_ID);
                BlogInfo bInfo = new BlogInfo();
                bInfo.blogid = blog.ProfileId.ToString();
                bInfo.blogName = blog.BlogName;
                bInfo.url = SiteSettings.BaseUrl + "blogs/" + blog.BlogName;
                infoList.Add(bInfo);


                return infoList.ToArray();

            }

            throw new XmlRpcFaultException(0, "User is not valid!");

        }

        UserInfo IMetaWeblog.GetUserInfo(string key, string username, string password)
        {

            if (ValidateUser(username, password))
            {

                UserInfo info = new UserInfo();
                StrategyProxyUser proxyUser = new StrategyProxyUser();                
                if (proxyUser.IsMembershipUsed)
                {
                    MembershipUser member=Membership.GetUser(username);
                    info.email = member.Email;
                    info.userid = member.ProviderUserKey.ToString();
                }
                else
                {
                    MemberFacade memberFacade = new MemberFacade();
                    SocialShare_Members member = memberFacade.ValidateUser(username, password);
                    if (member != null)
                    {
                        info.email = member.Email;
                        info.userid = member.Member_Id.ToString();
                        info.nickname = member.ScreenName;

                        SocialShare_MemberProfiles profile = memberFacade.GetUserProfileByID(member.Member_Id.ToString());
                        if (profile != null)
                        {
                            info.firstname = profile.First_Name;
                            info.lastname = profile.Last_Name;
                        }
                    }
                    else
                    {
                        throw new XmlRpcFaultException(0, "User is not valid!");
                    }

                }             

                return info;

            }

            throw new XmlRpcFaultException(0, "User is not valid!");

        }

        #endregion

        #region Private Methods

        private StrategyProxyUser GetUser(string username, string password)
        {
            StrategyProxyUser proxyUser = new StrategyProxyUser();
            if (proxyUser.IsMembershipUsed)
            {
                MembershipUser member = Membership.GetUser(username);
                proxyUser.User_ID = member.ProviderUserKey.ToString();
            }
            else
            {
                MemberFacade memberFacade = new MemberFacade();
                SocialShare_Members member = memberFacade.ValidateUser(username, password);
                if (member != null)
                {
                    proxyUser.User_ID = member.Member_Id.ToString();
                }
                else
                {
                    throw new XmlRpcFaultException(0, "User is not valid!");
                }

            }

            return proxyUser;
        }
        private bool ValidateUser(string username, string password)
        {

            bool result = false;
            StrategyProxyUser proxyUser = new StrategyProxyUser();
            if (proxyUser.IsMembershipUsed)
            {
                result=Membership.ValidateUser(username, password);
            }
            else
            {
                MemberFacade memberFacade = new MemberFacade();
                SocialShare_Members member = memberFacade.ValidateUser(username, password);
                if (member != null)
                {
                    result = true;
                }
                else
                {
                    result = false;
                }

            }


            return result;

        }

        #endregion

    }
}